// pages/collection/collection.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    showAnalysis:false,
    showBtn:true,
    question:null,
    answerList:[-1,-1,-1,-1],
    bookId:0,
    index:0,
    isRight :-1,
    source:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let app = getApp()
    let answerList = this.data.answerList
    let bookId = options.bookId
    let index = options.index
    this.setData({
      bookId:bookId,
      index:index,
      source:options.source,
    })
    if(options.source == "error"){
      this.setData({isRight:0})
      let selectedOption = app.globalData.errorSource.selectedOption
      let rightOption = app.globalData.errorSource.rightOption
      let multiple = app.globalData.errorSource.multiple
      if(multiple == 0){
        let optionNum = 0
        switch(selectedOption){
          case 'A':
            optionNum = 0
            break
          case 'B':
            optionNum = 1
            break
          case 'C':
            optionNum = 2
            break
          case 'D':
            optionNum = 3
            break
        }
        if(selectedOption == rightOption){
          answerList[optionNum] = 1
        }else{
          answerList[optionNum] = 0
        }
      }else{
        let arr = selectedOption.split("")
        for(let i = 0; i < arr.length; ++i){
          let optionNum = 0
          switch(arr[i]){
            case 'A':
              optionNum = 0
              break
            case 'B':
              optionNum = 1
              break
            case 'C':
              optionNum = 2
              break
            case 'D':
              optionNum = 3
              break
          }
          if(rightOption.indexOf(arr[i]) != -1){
            answerList[optionNum] = 1
          }else{
            answerList[optionNum] = 0
          }
        }
      }
      this.setData({
        showAnalysis:false,
        showBtn:true,
        question:app.globalData.errorSource,
        answerList:answerList,
      })
    }else if(options.source == "collect"){
      let selectedOption = app.globalData.errorSource.selectedOption
      let rightOption = app.globalData.errorSource.rightOption
      

      let multiple = app.globalData.errorSource.multiple
      if(selectedOption == "您未作答"){
        
      }else{
        if(selectedOption == rightOption){
          this.setData({isRight:1})
        }else{
          this.setData({isRight:0})
        }
        if(multiple == 0){
          let optionNum = 0
          switch(selectedOption){
            case 'A':
              optionNum = 0
              break
            case 'B':
              optionNum = 1
              break
            case 'C':
              optionNum = 2
              break
            case 'D':
              optionNum = 3
              break
          }
          if(selectedOption == rightOption){
            answerList[optionNum] = 1
          }else{
            answerList[optionNum] = 0
          }
        }else{
          let arr = selectedOption.split("")
          for(let i = 0; i < arr.length; ++i){
            let optionNum = 0
            switch(arr[i]){
              case 'A':
                optionNum = 0
                break
              case 'B':
                optionNum = 1
                break
              case 'C':
                optionNum = 2
                break
              case 'D':
                optionNum = 3
                break
            }
            if(rightOption.indexOf(arr[i]) != -1){
              answerList[optionNum] = 1
            }else{
              answerList[optionNum] = 0
            }
          }
        }
      }
      
      
      this.setData({
        showAnalysis:false,
        showBtn:true,
        question:app.globalData.errorSource,
        answerList:answerList,
      })

      
    }
  },
  showAnalysis:function(){
    this.setData({
      showAnalysis:true,
      showBtn:false,
    })
  },
  del:function(event){
    let practiceId = event.currentTarget.dataset.id
    let app = getApp()

    wx.request({
      url: app.globalData.url + '/question/delete',
      data:{
        openId: app.globalData.userInfo.openid ,
        practiceId:practiceId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
        let status = result.data.status
        let delerror = {"index":this.data.index, "hasmoved":false}
        

        app.globalData.delerror = delerror
        console.log(status)
        if(status == 200){
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            duration: 1000
          })          
        }else{
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 1000
          })     
        }
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})